home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / OpenPreferencesFolder.c < prev    next >
Text File  |  1995-07-26  |  3KB  |  108 lines

  1. /*
  2. OpenPreferencesFolder.c
  3.  
  4. Open the Preferences folder (in the Macintosh System Folder), creating it if
  5. necessary.
  6.  
  7. All the Macintosh-dependent code is conditionally compiled, anticipating that
  8. code will be added to support other operating systems, especially DOS and
  9. Windows.
  10.  
  11. OpenPreferencesWD() is a stand-alone routine that finds the Preferences folder
  12. (creating it if necessary) and allocates to it a working directory number that
  13. should subsequently be deallocated by calling CloseWD().
  14.  
  15. The purpose of OpenPreferencesFolder() and ClosePreferencesFolder() is to
  16. isolate the Macintosh dependence to this file, on the premise that many programs
  17. will want to access files stored in the  Preferences folder, and that such
  18. programs might be portable to other computers if a new routine is supplied to
  19. open an appropriate "Preferences" directory. The real work is done by
  20. OpenPreferencesWD(). OpenPreferencesFolder() and ClosePreferencesFolder() share
  21. hidden state variables.
  22.  
  23. The FindFolder routine appeared with System 7, but, according to THINK
  24. Reference, under System 6 it's implemented by glue code inserted by the compiler
  25. (THINK C 5 or MPW 3.2 or better), but the glue code will only give us the System
  26. Folder.
  27.  
  28. NOTE:
  29. This routine calls several Apple routines for which the glue is in THINK C's
  30. MacTraps2, not in MacTraps. So be sure to add both to your project if you use
  31. OpenPreferencesFolder. If you forget you'll get an error message from the Linker.
  32.  
  33. HISTORY:
  34. 6/13/93    dgp wrote it.
  35. 9/15/93    dgp    made Mac-specific code conditional.
  36. */
  37. #include "VideoToolbox.h"
  38. #if MAC_C
  39.     //#include <Files.h>
  40.     #include <Folders.h>
  41.     //#include <Errors.h>
  42.     static short oldVRefNum,wdRefNum;
  43. #endif
  44.  
  45. OSErr OpenPreferencesFolder(void)
  46. {
  47.     int error=1;
  48.  
  49.     #if MAC_C
  50.         error=GetVol(NULL,&oldVRefNum);
  51.         if(error)return error;
  52.         error=OpenPreferencesWD(&wdRefNum);
  53.         if(error)return error;
  54.         error=SetVol(NULL,wdRefNum);
  55.     #endif
  56.     return error;
  57. }
  58.  
  59. OSErr ClosePreferencesFolder(void)
  60. {
  61.     int error=1;
  62.  
  63.     #if MAC_C
  64.         error=SetVol(NULL,oldVRefNum);
  65.         if(error)return error;
  66.         error=CloseWD(wdRefNum);
  67.     #endif
  68.     return error;
  69. }
  70.  
  71. #if MAC_C
  72.     OSErr OpenPreferencesWD(short *wdRefNumPtr)
  73.     /*
  74.     Gets a working directory number for the Preferences folder in the active
  75.     System Folder. A Preferences folder is created if it doesn't already exist.
  76.     */
  77.     {
  78.         short vRefNum;
  79.         long dirID,createdDirID;
  80.         int error;
  81.         WDPBRec pb;
  82.         static unsigned char prefs[]="\pPreferences";
  83.     
  84.         #if 0    /* Support System 7 only */
  85.             error=FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder
  86.                 ,&vRefNum,&dirID);
  87.             if(error)return error;
  88.             error=OpenWD(vRefNum,dirID,0,wdRefNumPtr);
  89.         #else    /* Support both Systems 6 and 7 */
  90.             error=FindFolder(kOnSystemDisk,kSystemFolderType,kDontCreateFolder
  91.                 ,&vRefNum,&dirID);
  92.             if(error)return error;
  93.             pb.ioWDProcID=0;
  94.             pb.ioNamePtr=prefs;
  95.             pb.ioWDDirID=dirID;
  96.             pb.ioVRefNum=vRefNum;
  97.             error=PBOpenWD(&pb,0);
  98.             if(error==fnfErr){
  99.                 error=DirCreate(vRefNum,dirID,prefs,&createdDirID);
  100.                 if(error)return error;
  101.                 pb.ioVRefNum=vRefNum;
  102.                 error=PBOpenWD(&pb,0);
  103.             }
  104.             *wdRefNumPtr=pb.ioVRefNum;
  105.         #endif
  106.         return error;
  107.     }
  108. #endif